package com.anolesoft.amljmes.billing.service.mapper;

import cn.hutool.core.util.StrUtil;
import com.anolesoft.amljmes.billing.controller.GdyjDykhxController;
import com.anolesoft.amljmes.billing.dto.GdyjDykhxDto;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;

import java.util.List;

@Mapper
public interface GdyjDykhxServiceMapper extends BaseMapper<GdyjDykhxDto> {
    // 查询列表
    @SelectProvider(type=createSQL.class, method = "queryGdyjDykhxSQL")
    List<GdyjDykhxDto> queryGdyjDykhxSQL(GdyjDykhxController.DemoVO demoVO);
    public static class createSQL {
        public static String queryGdyjDykhxSQL(GdyjDykhxController.DemoVO demoVO){
            return new SQL(){
                {
                    StringBuilder sql = new StringBuilder();
                    sql.append(" a.objuid as key,a.objuid, a.part_drawing_no, b.name cjr, a.cjsj, a.factory_id, 0 as pageNo,10 as pageSize from mes_gdyj_dykhx a left join stf_employee b on a.cjr = b.objuid where a.delete_tag=0 ");
                    if(!StrUtil.isEmptyOrUndefined(demoVO.getPartDrawingNo())){
                        String partDrawingNo = demoVO.getPartDrawingNo();
                        sql.append(" and upper(a.part_drawing_no) = trim(upper(#{partDrawingNo, jdbcType=VARCHAR}))");
                    }
                    if(!StrUtil.isEmptyOrUndefined(demoVO.getFactoryId())){
                        String factoryId = demoVO.getFactoryId();
                        sql.append(" and upper(a.factory_id) = upper(#{factoryId, jdbcType=VARCHAR})");
                    }
                    SELECT(sql.toString());
                }
            }.toString();
        }
    }

    // 新增
    @Insert("    insert into mes_gdyj_dykhx" +
            "      (cjr, factory_id, part_drawing_no)" +
            "    values" +
            "      (#{cjr, jdbcType=VARCHAR}, #{factoryId, jdbcType=VARCHAR}, #{partDrawingNo, jdbcType=VARCHAR})"
    )
    int addGdyjDykhx(GdyjDykhxDto inFoDtoParam);

    //批量删除
    @Update("update mes_gdyj_dykhx set delete_tag = 1,delete_time = sysdate,delete_person = #{scr, jdbcType=VARCHAR} where objuid = #{objuid, jdbcType=VARCHAR}" )
    int delete(@Param("objuid") String objuid,@Param("scr") String scr);

    // 验证名称重复
    @Select(" select count(1) from mes_gdyj_dykhx where part_drawing_no = trim(#{partDrawingNo, jdbcType = VARCHAR}) and factory_id = #{factoryId, jdbcType = VARCHAR} and delete_tag=0 ")
    int isGdyjDykhxName(GdyjDykhxDto inFoDtoPara);
}
